﻿using System;
using System.Collections.Generic;
using DMS.BaseClass;
using DMS.Data;
using System.Linq;

namespace DMS.Business.Authority.Impl
{
    public class AuthorityFacadeImpl:IAuthorityFacade
    {
        private readonly DMSEntities _db = new DMSEntities();

        /// <summary>
        /// 通过actorId、actorTpye获取权限列表，包括page/function/data权限
        /// </summary>
        /// <param name="actorId"></param>
        /// <param name="actorTpye"></param>
        /// <returns></returns>
        public IList<PRM_ControlObjects> GetPermissions(string actorId, Enum actorTpye)
        {
            var controlObjects = from co in _db.PRM_ControlObjects
                                 join pl in _db.PRM_PermissionList on co.ControlID equals pl.ControlID
                                 where
                                     pl.ActorID == actorId && pl.ActorType == actorTpye.ToString() &&
                                     co.IsDeleted == false
                                 select co;

            return controlObjects.ToList();
        }
        /// <summary>
        /// 通过用户ID及URL检查权限，包括授权给角色、用户、部门的page/function/data权限
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public bool CheckUrlPermission(string userId,string url)
        {
            if(!string.IsNullOrEmpty(url))
            {
                string userType = ActorType.User.ToString();
                string roleType = ActorType.Role.ToString();
                string deptType = ActorType.Dept.ToString();

                var isUnderControl = (from co in _db.PRM_ControlObjects
                                      where co.Url == url && co.IsDeleted == false
                                      select co).Count() > 0;
                if(isUnderControl)
                {
                    var controlObjectsCount = (from co in _db.PRM_ControlObjects
                                               join pl in _db.PRM_PermissionList on co.ControlID equals pl.ControlID
                                               where
                                                   pl.ActorID == userId && pl.ActorType == userType &&
                                                   co.Url.Equals(url) &&
                                                   co.IsDeleted == false
                                               select co).Union(
                                               from co in _db.PRM_ControlObjects
                                               join pl in _db.PRM_PermissionList on co.ControlID equals pl.ControlID
                                               join or1 in _db.ORG_RoleMembers on pl.ActorID equals or1.RoleID
                                               join ou in _db.ORG_Users on or1.MemberID equals ou.UserID
                                               where
                                                   ou.UserID == userId && pl.ActorType == roleType &&
                                                   co.Url.Equals(url) &&
                                                   or1.MemberType == 0 &&
                                                   co.IsDeleted == false
                                               select co
                    ).Union(
                        from co in _db.PRM_ControlObjects
                        join pl in _db.PRM_PermissionList on co.ControlID equals pl.ControlID
                        join ou in _db.ORG_Users on pl.ActorID equals ou.DeptID
                        where ou.UserID == userId && pl.ActorType == deptType &&
                              co.Url.Equals(url) &&
                              co.IsDeleted == false
                        select co
                    ).Count();
                    return controlObjectsCount > 0;
                }
                return true;
            }
            return false;
        }
    }
}
